<?php 
namespace app\model;
use think\Model;
use think\Db;

class UserBill extends Model
{   

    public static function saveData($save_data){
        if ( $save_data['amount'] == 0 ) {
            return true;
        }

        $balance_field = $save_data['collect'].'_balance';
        $uinfo = Db::name('user')->field(['id',$balance_field])->where('id',$save_data['user_id'])->find();
        if ( empty($uinfo) ) {
            return true;
        }
        $model = Db::name('user_bill');
        $save_data['bill_sn'] = uuidCreate();
        $save_data['ctime'] = time();
        $save_data['old_balance'] = $uinfo[$balance_field];
        $save_data['new_balance'] = round($uinfo[$balance_field] + $save_data['amount'],3);
        if (isset($save_data['params'])) {
            $save_data['params'] = json_encode($save_data['params'],JSON_UNESCAPED_UNICODE);
        }

        if ( $model->insert($save_data) ) {
            Db::name('user')->where('id',$save_data['user_id'])->inc($balance_field,$save_data['amount'])->update();
            return true;
        }else{
            return false;
        }
    }

    public static function getCollectCn($key){
        $list = ['klingai'=>'可灵AI','streamlake'=>'快手万擎','volcengine'=>'火山方舟'];
        return $list[$key];
    }

    public static function getCollectList(){
        $list = ['klingai'=>'可灵AI(余额)','streamlake'=>'快手万擎(tokens)','volcengine'=>'火山方舟(tokens)'];
        return $list;
    }
}


 ?>